package graduate.cslg.com.universityshopping.mainPage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.List;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.adapter.LostInfoAdapter;
import graduate.cslg.com.universityshopping.entity.LostCardBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class GetLostActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private List<LostCardBean> mLostCardBeans;
    private LostInfoAdapter mLostInfoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_lost);

        initViews();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        getLostCardInfo();
    }

    private void initViews() {
        mRecyclerView = findViewById(R.id.lost_found_recycler);
    }

    private void getLostCardInfo() {
        RetrofitDevelop.getTestService()
                .getLostCardInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<LostCardBean>>() {
                    @Override
                    public void onCompleted() {
                        mLostInfoAdapter = new LostInfoAdapter(mLostCardBeans);
                        mRecyclerView.setAdapter(mLostInfoAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<LostCardBean> lostCardBeans) {
                        mLostCardBeans = lostCardBeans;
                    }
                });
    }
}
